package com.wish.finance.controller;


import com.wish.common.core.domain.R;
import com.wish.common.core.usconfig.common.MsgResult;
import com.wish.finance.entity.JrProduct;
import com.wish.finance.entity.JrSfLoan;
import com.wish.finance.entity.vo.*;
import com.wish.finance.service.IJrSfLoanService;
import com.wish.finance.service.ITSfContractService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 收房贷申请信息 前端控制器
 * </p>
 *
 * @author xiaocui
 * @since 2022-04-06
 */
@RestController
@RequestMapping("jrsfloan")
public class JrSfLoanController {

    @Autowired
    private IJrSfLoanService jrSfLoanService;

    /**
     * @Date 2022/4/6 13:13
     * @deprecated 申请收房贷：收房合同查询(选中的一条)
     **/
    @GetMapping(value = "/searchSfAllById")
    public MsgResult searchSfAllById(Long sfContractId) {
        SfContractDetailsResponseDTO sfContractDetailsResponseDTO = jrSfLoanService.getSfContract(sfContractId);
        // 拼接房产信息   = 小区名称+ 房产信息
        String houseName = sfContractDetailsResponseDTO.getPremiseName() + sfContractDetailsResponseDTO.getHouseName();
        sfContractDetailsResponseDTO.setHouseName(houseName);
        return MsgResult.succeed(sfContractDetailsResponseDTO);
    }


    /**
     * 根据jr_product_type金融产品类型查找出未删除的所有收房贷;类型
     *
     * @Date 2022/4/6 15:00
     * @deprecated 金融产品选择
     **/
    @GetMapping("/searchProduct")
    public MsgResult searchProduct() {
        List<JrProduct> productList = jrSfLoanService.searchProduct(2);
        return MsgResult.succeed(productList);
    }

    /**
     * @Date 2022/4/6 15:16
     * @deprecated 金融产品选择(其中一条)
     **/
    @GetMapping(value = "/searchProductById")
    public MsgResult searchProductById(Long id) {
        return MsgResult.succeed(jrSfLoanService.searchProductById(id));
    }


    /**
     * @Date 2022/4/6 15:19
     * @deprecated 申请收房贷：计算--分期
     **/
    @GetMapping(value = "/listSfLoan")
    public Map<String, Object> listSfLoan(SfLoanDetailRequestBean bean) {
        Map<String, Object> map = new HashMap<>();
        //收房贷计算
        SfContractDetailsResponseDTO sfentity = jrSfLoanService.getSfContractStagingCalculation(bean.getContractId(), bean.getProductId());
        //查询 - 分期计算详情
        SearchSfLoanDetailRequestDto sfdto = new SearchSfLoanDetailRequestDto();
        BeanUtils.copyProperties(bean, sfdto);

        sfdto.setBalance(new BigDecimal(bean.getBalance()));    // 借款金额
        sfdto.setSxBalance(new BigDecimal(bean.getSxBalance())); // 授信额
        sfdto.setPeriods(bean.getPeriods());    // 期数
        sfdto.setStartDate(bean.getStartDate()); // 合同开始日
        sfdto.setEndDate(bean.getEndDate());    // 合同结束日

        SearchSfLoanDetailsResponseDto sfr = jrSfLoanService.getSfLoanDetail(sfdto);
        //分期计算
        map.put("sfr", sfr);
        map.put("sfentity", sfentity);
        return map;
    }


    /**
     * @deprecated 申请收房贷：保存
     * @Date 2022/4/6 17:18
     **/
    @PostMapping(value = "/savesf")
    public MsgResult saveContract(@RequestBody SaveSfLoanRequestDto sfLoanForm) {
        System.err.println(sfLoanForm+"====================================");
        if (sfLoanForm.getPeriods() > sfLoanForm.getPeriodsA()) {
            return MsgResult.fail(500,"借款期限大于合同有效期限",null);
        }
        JrProduct jrProduct =  jrSfLoanService.selectById(sfLoanForm.getProductId());
        if(jrProduct.getInstalmentPeriod() < sfLoanForm.getPeriods()) {
            return MsgResult.fail(500,"借款期限大于金融产品期限",null);
        }
        JrSfLoan jrSfLoan = new JrSfLoan();
        jrSfLoan.setSfLoanOrderNo(sfLoanForm.getSfContractNo());
        jrSfLoan.setApplyState(1);  //申请中
        jrSfLoan.setDelFlg(0);
        jrSfLoan.setSfContractId(sfLoanForm.getId());
        jrSfLoan.setLoanAmount(sfLoanForm.getSxBalance());
        jrSfLoan.setMonthLimit(sfLoanForm.getPeriods());
        jrSfLoan.setJrProductId(sfLoanForm.getProductId());
        jrSfLoan.setReLeaseCount(sfLoanForm.getPayAbleCount());
        jrSfLoan.setRpAmountCount(sfLoanForm.getRePaymentCount());
        jrSfLoan.setAgInterestRate(sfLoanForm.getInterestCount());
        jrSfLoan.setRemark(sfLoanForm.getRemarks());
        jrSfLoan.setSerCharge(sfLoanForm.getSerCharge());
        jrSfLoan.setRate(sfLoanForm.getRate());

        boolean save = jrSfLoanService.save(jrSfLoan);
        System.err.println(save);
        return MsgResult.succeed();
    }

}
